package moonstruckdrops.serviceImpl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;

import moonstruckdrops.datastore.provider.BlogEntoryProvider;
import moonstruckdrops.datastore.provider.ProfileProvider;
import moonstruckdrops.dto.EntryDto;
import moonstruckdrops.dto.ProfileDto;
import moonstruckdrops.model.BlogBasicParts;
import moonstruckdrops.page.BlogComentPage;
import moonstruckdrops.service.BlogMainService;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;

import com.google.appengine.api.datastore.Text;

public class BlogMainServiceImpl implements BlogMainService,Serializable{

	private static final long serialVersionUID = 1L;

	public BlogBasicParts BlogMainPageinit(){
		ProfileProvider provider= new ProfileProvider();
		List<ProfileDto> profileList=provider.getProfile();//getProfile(0,1);
		ProfileDto profile=profileList.get(0);
		return new BlogBasicParts(makeLabel("Author",profile.getBlogAuthor()),makeLabel("BlogTitle",profile.getBlogTitle())
		,makeLabel("Coment",profile.getBlogComent()),makeMultiLabel("Profile",profile.getBlogAuthorProfile()));
	}
	
	public DataView<EntryDto> EntryView(BlogEntoryProvider entry,final BlogBasicParts BlogParts){
		DataView<EntryDto> list=new DataView<EntryDto>("list",entry,5){
			private static final long serialVersionUID = 1L;
			protected void populateItem(Item<EntryDto> item){
				EntryDto entity=item.getModelObject();
				final String EntryDate =new SimpleDateFormat("yyyy-MM-dd").format(entity.getDate().getTime());
				final String Title=entity.getTitle();
				final Text Entry=entity.getEntry();
				final Long EntryID=entity.getId();
				item.add(makeLabel("Date",EntryDate));
				item.add(makeLabel("Title",Title));
				item.add(makeMultiLabel("Entry",Entry).setEscapeModelStrings(false));
				Link<Void> linkc=new Link<Void>("ComentToLink"){
					private static final long serialVersionUID = 1L;
					public void onClick(){
						BlogComentPage ComentPage= new BlogComentPage(BlogParts,EntryID,EntryDate,Title,Entry);
						setResponsePage(ComentPage);
					}
				};
				item.add(linkc);
			}
		};
		return list;
	}

	
	private Label makeLabel(String WicketID,String Value){
		return new Label(WicketID,Value);
	}
	
	private MultiLineLabel makeMultiLabel(String WicketID,Text Value){
		return new MultiLineLabel(WicketID,Value.getValue());
	}
}
